//
//  HBHeadView.m
//  HubeiHotel
//
//  Created by Can on 2018/7/23.
//  Copyright © 2018年 张露杰. All rights reserved.
//

#import "HBHeadView.h"
#import "HBHMainGetMoreView.h"
#import "HBHMainGetMoreNoImageView.h"

@implementation HBHeadView

- (instancetype)initWithFrame:(CGRect)frame bannerImages:(NSArray *)bannerImages dataArray:(NSArray *)dataArray zhiArray:(NSArray *)zhiArray
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame = frame;
        
        _bannerImages = bannerImages;
        
        [self creatView];
        
        [self creatWaterfallsFlow:SCREENW/5 dataArray:dataArray];
        
        [self creatScrollView:zhiArray];
        
        [self creatIconView];
    }
    return self;
}

- (void)creatView
{
    _scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREENW, 180*screenW_6) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
    _scrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _scrollView.currentPageDotColor = [UIColor whiteColor];
    _scrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    _scrollView.autoScrollTimeInterval = 4.0f;
    _scrollView.userInteractionEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.tag = 1;
    [self addSubview:_scrollView];
    //加载的图片数组
    _scrollView.imageURLStringsGroup = _bannerImages;
}

//轮播图片点击事件
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index;
{
    if (cycleScrollView.tag == 1) {
        if (_bannerblock) {
            _bannerblock(index);
        }
    }else{
        if (_zhiblock) {
            _zhiblock(index);
        }
    }
}

- (void)creatWaterfallsFlow:(CGFloat)width dataArray:(NSArray *)dataArray
{
    CGFloat itemWidth = width;
    NSInteger col = 5;
    CGFloat margin = 0;
    CGFloat itemHeight = 75*screenW_6;
    
    UIButton *last = nil;
    
    for (int i = 0 ; i < dataArray.count; i++) {
        UIButton *item  = [UIButton buttonWithType:UIButtonTypeCustom];

        CGFloat top = margin + (i/col)*(itemHeight + margin) + 180*screenW_6;
        if (!last || (i%col) == 0) {  //last为nil  或者(i%col) == 0 说明换行了 每行的第一个确定它距离左边边缘的距离
            item.frame = CGRectMake(0, top, itemWidth, itemHeight);
        }else{
            item.frame = CGRectMake(CGRectGetMaxX(last.frame), top, itemWidth, itemHeight);
        }
        item.backgroundColor = [UIColor whiteColor];
        item.tag = i;
        [item addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:item];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(17*screenW_6, 13*screenW_6, 40*screenW_6, 40*screenW_6)];
        if (i == 9) {
            imageView.image = [UIImage imageNamed:@"all"];
        }else{
            
            [imageView sd_setImageWithURL:[NSURL URLWithString:dataArray[i][@"image"]] placeholderImage:nil];
            
//            NSString * imageUrl = [dataArray[i][@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//            UIImage *newImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:imageUrl];//用地址去本地找图片
//            if (newImage != nil) {//如果本地有
//                // newImage就是图片
//                imageView.image = newImage;
//
//            } else {//如果本地没有
//                //下载图片
//                [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//                    //这里是处理下载进度的,好像没必要管他
//                } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
//                    if (image) {//下载完成后
//                        //同上处理
//                        imageView.image = image;
//                    }
//                }];
//            }
        }
        [item addSubview:imageView];
        
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 60*screenW_6, width, 12*screenW_6)];
        lable.text = dataArray[i][@"name"];
        lable.textColor = [UIColor colorWithHexString:@"#666666"];
        lable.font = [UIFont systemFontOfSize:12*screenW_6];
        lable.textAlignment = NSTextAlignmentCenter;
        [item addSubview:lable];

        last = item;
    }
}

- (void)addButton:(UIButton *)btn
{
    if (_datablock) {
        _datablock(btn.tag);
    }
}

- (void)creatScrollView:(NSArray *)dataArray
{
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 346*screenW_6, SCREENW, .5)];
    lineView.backgroundColor = backColor;
    [self addSubview:lineView];
    
    UIImageView *imageV = [TomamaUI createImageView:CGRectMake(33*screenW_6/2, 346*screenW_6 + 15*screenW_6, 33*screenW_6, 33*screenW_6) backGroundImageV:@"Main_news"];
    imageV.userInteractionEnabled = YES;
    [self addSubview:imageV];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTap:)];
    [imageV addGestureRecognizer:tap];
    
    UIView *lineUpRightView = [[UIView alloc]initWithFrame:CGRectMake(66*screenW_6, 346*screenW_6, .5, 33*screenW_6)];
    lineUpRightView.backgroundColor = backColor;
    lineUpRightView.center = CGPointMake(lineUpRightView.center.x, imageV.center.y);
    [self addSubview:lineUpRightView];
    
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(80*screenW_6, 356*screenW_6, SCREENW - 100*screenW_6, 40*screenW_6) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    _cycleScrollView.tag = 2;
    _cycleScrollView.autoScrollTimeInterval = 4.0f;
    _cycleScrollView.userInteractionEnabled = YES;
    _cycleScrollView.delegate = self;
    _cycleScrollView.scrollDirection = NO;
    _cycleScrollView.onlyDisplayText = YES;
    _cycleScrollView.titleLabelTextFont = [UIFont systemFontOfSize:14];
    _cycleScrollView.titleLabelHeight = 40*screenW_6;
    _cycleScrollView.titleLabelBackgroundColor = [UIColor whiteColor];
    _cycleScrollView.titleLabelTextColor = TextBlack;
    _cycleScrollView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.cycleScrollView];
    self.cycleScrollView.titlesGroup = dataArray;
    self.cycleScrollView.imageURLStringsGroup = dataArray;
}

- (void)doTap:(UITapGestureRecognizer *)gesture{
    if (_imageblock) {
        _imageblock(0);
    }
}

- (void)creatIconView
{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 346*screenW_6 + 60*screenW_6, SCREENW, 190*screenW_6)];
    backView.backgroundColor = backColor;
    [self addSubview:backView];
    
    HBHMainGetMoreNoImageView *view = [[HBHMainGetMoreNoImageView alloc]initWithFrame:CGRectMake(5*screenW_6, 5*screenW_6, 0, 0) complete:^(NSInteger tagNumber){
        if (_iconblock) {
            _iconblock(tagNumber);
        }
    }];
    [backView addSubview:view];
    
    NSArray *titleImages = @[@"Main_meili_1",@"Main_meili_2"];
    NSArray *titles = @[@"满足你的一切幻想",@"多重优惠等你来享"];
    NSArray *icons = @[@"Main_icon1",@"Main_icon2"];

    for (int i = 0; i < 2; i++) {
        HBHMainGetMoreView *moreView = [[HBHMainGetMoreView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(view.frame) + 5*screenW_6, 5*screenW_6 + 87.5*i*screenW_6, 180*screenW_6, 85*screenW_6) titleImage:titleImages[i] title:titles[i] icon:icons[i] complete:^(NSInteger tagNumber){
            if (_iconblock) {
                _iconblock(tagNumber);
            }
        }];
        moreView.tag = i+1;
        [backView addSubview:moreView];
    }
}

@end
